package com.idbk.chargestation.activity.charge;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.idbk.chargestation.R;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonPointPile;
import com.idbk.chargestation.bean.JsonQrScan;
import com.idbk.chargestation.bean.JsonQrScanList;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.Const;
import com.idbk.chargestation.util.GsonUtils;

/**
 * 当前和预约电桩选择界面
 * @author zhx
 *
 */

public class ActivityChoosePile extends BaseActivity implements OnClickListener {

	private Context mContext;
	private TextView mPoint, mSn, mStyle,mCost,mPerformance;
	private TextView mBookPoint,mBookSn,mBookStyle,mBookCost,mBookPerformance;
	private JsonQrScanList result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		mContext = ActivityChoosePile.this;

		setContentView(R.layout.activity_charge);

		setupView();

		setupData();
	}

	public void setupView() {
		setupToolBar2();//显示标题栏
		//当前电桩
		mPoint = (TextView) findViewById(R.id.point);
		mSn = (TextView) findViewById(R.id.sn);
		mStyle = (TextView) findViewById(R.id.style);
		mCost = (TextView) findViewById(R.id.cost);
		mPerformance = (TextView) findViewById(R.id.performance);

		//预约电桩
		mBookPoint = (TextView) findViewById(R.id.book_point);
		mBookSn = (TextView) findViewById(R.id.book_sn);
		mBookStyle = (TextView) findViewById(R.id.book_style);
		mBookCost = (TextView) findViewById(R.id.book_cost);
		mBookPerformance = (TextView) findViewById(R.id.book_performance);

		findViewById(R.id.layout_current).setOnClickListener(this);
		findViewById(R.id.layout_book).setOnClickListener(this);
		findViewById(R.id.btn_sure).setOnClickListener(this);
	}

	private void setupData() {
		Bundle bundle = this.getIntent().getExtras();
		String pileSnCurrent = bundle.getString(Const.KEY_PILE_SN);
		showMyProgressDialog(true, false, "提交中...");
		ChargeStationAPI.qrScan(pileSnCurrent, mResponse);
	}

	
	@Override
	public void onClick(View view) {
		switch(view.getId()) {
			case R.id.btn_sure:
				JsonQrScan pointPile = new JsonQrScan();
				pointPile.pointName = result.compares.get(1).pointName;
				pointPile.pileSn = result.compares.get(1).pileSn;
				pointPile.performance = result.compares.get(1).performance;
				pointPile.pileType = result.compares.get(1).pileType;
				pointPile.priceTypeEnum = result.compares.get(1).priceTypeEnum;
				pointPile.chargeSpeed = result.compares.get(1).chargeSpeed;
				pointPile.gunA = result.compares.get(1).gunA;
				pointPile.gunB = result.compares.get(1).gunB;
				pointPile.tariff = result.compares.get(1).tariff;
				pointPile.balance = result.compares.get(1).balance;

				Intent intent = new Intent();
				intent.putExtra(JsonQrScan.KEY, pointPile);
				if(result.compares.get(1).pileType == JsonPointPile.PILE_TYPE_DC
					|| result.compares.get(1).pileType == JsonPointPile.PILE_TYPE_AC_SINGLE_PHASE
					|| result.compares.get(1).pileType == JsonPointPile.PILE_TYPE_AC_THREE_PHASE) {
					intent.setClass(mContext, ActivitySingleGun.class);
				} else {
					intent.setClass(mContext, ActivityDoubleGun.class);
				}
				startActivity(intent);
				finish();
				break;
			case R.id.layout_current:
				ShowTariff(JsonQrScan.CHOOSE_PILE);
				break;
			case R.id.layout_book:
				ShowTariff(JsonQrScan.BOOK_PILE);
				break;
			default:
					break;
		}
	}

	private void ShowTariff(int flag) {
		if (result != null) {
			String title, msg;
			msg = result.compares.get(flag).tariffInformation(flag);
			if(flag == 1) {
				title = "#01";
			} else {
				title = "#02";
			}
			new AlertDialog.Builder(mContext)
			.setTitle(title)
			.setMessage(Html.fromHtml(msg))
			.setCancelable(true)
			.setNegativeButton(R.string.common_ok, null)
			.create()
			.show();
		} else {
			Toast.makeText(mContext, R.string.common_tip_resultfail,Toast.LENGTH_SHORT).show();
		}
		
	}
	

	private final EHttpResponse mResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			result = GsonUtils.toBean(JsonQrScanList.class, response);
			dismissMyProgressDialog();
			if(handleResponseStatus(result)) {
				mBookPoint.setText(result.compares.get(0).pointName);
				mBookSn.setText(result.compares.get(0).pileSn);
				mBookStyle.setText(result.compares.get(0).performance);
				mBookCost.setText(result.compares.get(0).getPriceTypeString());
				mBookPerformance.setText(result.compares.get(0).chargeSpeed);

				mPoint.setText(result.compares.get(1).pointName);
				mSn.setText(result.compares.get(1).pileSn);
				mStyle.setText(result.compares.get(1).performance);
				mCost.setText(result.compares.get(1).getPriceTypeString());
				mPerformance.setText(result.compares.get(1).chargeSpeed);
			}
		}		

		@Override
		public void onEErrorResponse(VolleyError error) {
			dismissMyProgressDialog();
			Toast.makeText(mContext, R.string.common_tip_resultfail, Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
			dismissMyProgressDialog();
		}	
	};

}
